package flyshoes.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import flyshoes.mybatis.entity.UserBaseInfo;
import flyshoes.mybatis.entity.UserDetailInfo;
import flyshoes.mybatis.entity.WeChatUser;
import flyshoes.mybatis.mapper.UserbaseinfoMapper;
import flyshoes.mybatis.mapper.WeChatLoginMapper;
import flyshoes.mybatis.service.serviceImpl.UserdetailinfoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.DispatcherServlet;

@Service
@ConditionalOnClass(DispatcherServlet.class)
public class WeChatLoginServiceImpl {
//    @Value("${jwt.secret}")
//    private String secret;

    @Autowired
    private UserdetailinfoServiceImpl detailinfoService;
    @Autowired
    private UserbaseinfoMapper userbaseinfoMapper;
    @Autowired
    private WeChatLoginMapper weChatLoginMapper;
    @Transactional
    public UserBaseInfo newWeChatUser(UserBaseInfo baseInfo,UserDetailInfo detailInfo, WeChatUser weChatUser) {
        detailInfo.setUserId(baseInfo.getId());
        detailinfoService.save(detailInfo);
        weChatUser.setUserId(baseInfo.getId());
        weChatLoginMapper.insert(weChatUser);
        return baseInfo;
    }
    @Transactional(readOnly = true)
    public UserBaseInfo queryByOpenId(String openId) {
        QueryWrapper<WeChatUser> qwwx = new QueryWrapper<>();
        qwwx.eq("open_id", openId);
        WeChatUser wx = weChatLoginMapper.selectOne(qwwx);
        if(wx == null) {
            return null;
        }else{
            return userbaseinfoMapper.selectById(wx.getUserId());//getCustmerId
        }
    }

}
